#
# 从金投网(https://www.cngold.org/)获取价格信息
#

from requests import get
from json import loads
import CapturerDb

#根据代码获取价格，代码由源网站提供
def getPrices(codes, priceName, priceUnit, price_info_list):
    print('开始获取金投网' + priceName + '数据...')
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36',
        'Referer':'https://m.cngold.org/'
    }
     #"https://m.cngold.org/"
    url = 'https://api.jijinhao.com/history/quotejs.htm?codes=' + codes + '&style=3&currentPage=1&pageSize=1&_=1716794288867'
    
    response = get(url, headers = headers)
    if response.status_code == 200:
        #print(response.text)
        jsonData = loads(response.text[15:])
            
        timeText = jsonData[0]['data'][0]['quote']['q59']
        priceText =jsonData[0]['data'][0]['quote']['q63']
        #print(priceName, timeText, '金投网', '全国', priceText, priceUnit)
        price_info_list.append(['金投网', '基金', url, '全国', timeText, priceName, '', priceText, priceUnit])
        # CapturerDb.updatePrice('金投网', '基金', url, '全国', timeText, priceName, '', priceText, priceUnit)

        print('获取金投网' + priceName + '信息完成!')
    else:
        print('获取金投网' + priceName + '信息失败!')